package samse.exam.test1;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class ListTestActivity extends Activity implements AdapterView.OnItemClickListener {
	private String[] items = {
			"test 1", "test string 2", "kljakljfklsajf "
	};
	ArrayList<IntentItem>	intentItems =  new ArrayList<IntentItem>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.list_test);		
		
				
		Intent intent = new Intent();		
		ComponentName compName = new ComponentName("samse.exam.test1", "samse.exam.test1.MainActivity");
		intent.setComponent(compName); 
		intent.setAction("android.intent.action.MAIN");
		IntentItem n1 = new IntentItem(intent, "TextView test");
		intentItems.add(n1);
				
		intent = new Intent();
		compName = new ComponentName("samse.exam.test1", "samse.exam.test1.AnimationTestActivity");
		intent.setComponent(compName); 
		intent.setAction("android.intent.action.MAIN");
		IntentItem n2 = new IntentItem(intent, "Animation test");
		intentItems.add(n2);
		
		intent = new Intent();
		compName = new ComponentName("samse.exam.test1", "samse.exam.test1.AdaptListActivity");
		intent.setComponent(compName); 
		intent.setAction("android.intent.action.MAIN");
		IntentItem n3 = new IntentItem(intent, "Adapter List test");
		intentItems.add(n3);
		
		intent = new Intent();
		compName = new ComponentName("samse.exam.test1", "samse.exam.test1.AdaptListActivity2");
		intent.setComponent(compName); 
		intent.setAction("android.intent.action.MAIN");
		IntentItem n4 = new IntentItem(intent, "Adapter List test #2");
		intentItems.add(n4);
		
		intent = new Intent();
		compName = new ComponentName("samse.exam.test1", "samse.exam.test1.PageChangerActivity");
		intent.setComponent(compName); 
		intent.setAction("android.intent.action.MAIN");
		IntentItem n5 = new IntentItem(intent, "Page Changer");
		intentItems.add(n5);
		
		IntentListAdapter adapter = new IntentListAdapter(this, R.layout.list_item, intentItems);
		
		final ListView lv = (ListView)findViewById(R.id.list);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(this);
		/*
		final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		// Adapter that shows a list of string resources
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.list_item, items);
        
		ListView lst = (ListView)findViewById(R.id.list);
		lst.setAdapter(adapter);*/
		
	}
	/*
	@Override
	public void onClick(View v) {
		ListView lv = (ListView)findViewById(R.id.list);
		if(lv!=null) {
			int n = lv.getCheckedItemPosition();
			if(n!=AdapterView.INVALID_POSITION) {
				IntentItem ii = intentItems.get(n);
				this.sendBroadcast(ii.intent);
			}
		}
	}*/
	private static final String TAG="ListTestActivity";
    public void onItemClick(AdapterView parent, View v, int position, long id) {
    	IntentItem ii = intentItems.get(position);
		this.startActivity(ii.intent);        
		Log.d(TAG, "sendBroadcast");
    }
    
    private class IntentListAdapter extends ArrayAdapter<IntentItem> {
		ArrayList<IntentItem> items;
		public IntentListAdapter(Context context, int resourceId, ArrayList<IntentItem> items) {
			super(context, resourceId, items);
			this.items = items;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.list_item, null);
			}
			IntentItem p = items.get(position);
			if (p != null) {
				TextView tt = (TextView) v.findViewById(R.id.text1);
				if (tt != null){
					tt.setText(p.getTitle());                           
				}
			}
			return v;
		}
	}
	
	private class IntentItem {
		private Intent intent;
		private String title;
		
		public IntentItem(Intent mIntent, String title) {
			this.intent = mIntent;
			this.title = title;
		}		
		public Intent getIntent() {
			return intent;
		}		
		public String getTitle() {
			return title;
		}				
	}
	
}
